﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using Library.MVCHelpers;
using Newtonsoft.Json;

namespace Library
{
    public static class Utilities
    {
        public static string ToDisplayString(this Enum enumValue)
        {
            var enumType = enumValue.GetType();
            var field = enumType.GetField(enumValue.ToString());
            var attributes = field.GetCustomAttributes(typeof(DisplayAttribute), false);
            return attributes.Length == 0
                ? enumValue.ToString()
                : ((DisplayAttribute)attributes[0]).Name;
        }

        public static async Task<string> UploadImages(string imageDirectoryUrl)
        {
            var resultList = new List<string>();
            foreach (string file in HttpContext.Current.Request.Files)
            {
                var fileContent = HttpContext.Current.Request.Files[file];
                if (fileContent != null && fileContent.ContentLength > 0)
                {
                    if (!Settings.ValidImageTypes.Contains(fileContent.ContentType))
                    {
                        return JsonConvert.SerializeObject(new ViewHelper.HttpStatusCodeJsonResult(HttpStatusCode.InternalServerError, NOWCulture.GetString("Please choose either a GIF, JPG or PNG image.")));
                    }

                    // get a stream
                    var stream = fileContent.InputStream;

                    // and optionally write the file to disk
                    var fileName = Path.GetFileName(fileContent.FileName);
                    if (!string.IsNullOrWhiteSpace(fileName))
                    {
                        var directoryPath = HttpContext.Current.Server.MapPath("~" + imageDirectoryUrl);
                        if (!Directory.Exists(directoryPath))
                        {
                            Directory.CreateDirectory(directoryPath);
                        }

                        var imagePath = Path.Combine(directoryPath, fileName);
                        using (var fileStream = File.Create(imagePath))
                        {
                            await stream.CopyToAsync(fileStream);
                            resultList.Add(imageDirectoryUrl + "/" + fileName);
                        }
                    }
                }
            }
            return JsonConvert.SerializeObject(resultList);
        }

        public static string UploadImage(string imageDirectoryUrl, HttpPostedFileBase image)
        {
            try
            {
                if (image != null && image.ContentLength > 0)
                {

                    if (!Settings.ValidImageTypes.Contains(image.ContentType))
                    {
                        return string.Empty;
                    }

                    var directoryPath = HttpContext.Current.Server.MapPath("~" + imageDirectoryUrl);
                    if (!Directory.Exists(directoryPath))
                    {
                        Directory.CreateDirectory(directoryPath);
                    }

                    var imagePath = Path.Combine(directoryPath, image.FileName);
                    image.SaveAs(imagePath);

                    return imageDirectoryUrl + "/" + image.FileName;
                }
                return string.Empty;
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex);
                return string.Empty;
            }
        }

        public static bool RemoveImage(string imageSrc)
        {
            try
            {
                var fileName = HttpContext.Current.Server.MapPath("~" + imageSrc);
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex);
            }
            return false;
        }
    }
}
